<?php


namespace app\admin\model;


use think\Model;

/**
 * Class HtnsOrder
 * @package app\admin\model
 * @desc 海淘女神-订单
 */
class HtnsOrder extends Model
{
    protected $connection = 'htns';
    protected $table = 'eb_store_order';
    // 支付类型
    public $payType = [
        'yue'    => '余额支付',
        'weixin' => '微信支付',
        'tlpay'  => '通联支付',
    ];
    // 订单状态
    public $orderStatus = [
        -2 => '退货成功',
        -1 => '申请退款',
        0  => '未支付',
        1  => '待收货',
        2  => '已收货',
        3  => '已完成',
    ];
    // 退款状态
    public $refundStatus = [
        0  => '未退款',
        1  => '未退款',
        2  => '已退款',
    ];

    // 退款状态
    public $orderTypes = [
        'normal'  => '快递配送订单',
        'store'  => '基站订单'
    ];

    // 订单搜索状态
    public $status = [
        0 => '未支付',
        1 => '未发货',
        2 => '待收货',
        3 => '已收货',
        4 => '已完成',
    ];
    // 获取搜索订单状态
    public function getStatus($order)
    {
        $status = '未支付';
        if(!$order['paid'] && !$order['status'] && !$order['refund_status'] && !$order['is_del']){
            $status = '未支付';
        }elseif ($order['paid'] && $order['status']==1 && in_array($order['refund_status'],[0,1,2,3]) && !$order['is_del']){
            $status = '待收货';
        }elseif ($order['paid'] && $order['status']==2 && in_array($order['refund_status'],[0,1,2,3]) && !$order['is_del']){
            $status = '已收货';
        }elseif ($order['paid'] && $order['status']==3 && in_array($order['refund_status'],[0,1,2,3]) && !$order['is_del']){
            $status = '已完成';
        }elseif ($order['paid'] && in_array($order['status'],[0,1,2,3,4]) && in_array($order['refund_status'],[0,1,2,3]) && !$order['is_del']){
            $status = '未发货';
        }elseif ($order['paid'] && $order['refund_status']==2 && !$order['is_del']){
            $status = '已退款';
        }elseif ($order['paid'] && in_array($order['refund_status'],[1,2,3,4]) && !$order['is_del']){
            $status = '退款中';
        }
        return $status;
    }
    // 获取订单类型
    public function getOrderType($order)
    {
        $type = '[普通订单]';
        if (isset($order['combination_id']) && $order['combination_id']) {
            $type = '[拼团订单]';
        } elseif (isset($order['pink_id']) && $order['pink_id']) {
            $type = '[拼团订单]';
        } elseif (isset($order['seckill_id']) && $order['seckill_id']) {
            $type = '[秒杀订单]';
        } elseif (isset($order['bargain_id']) && $order['bargain_id']) {
            $type = '[砍价订单]';
        }
        return $type;
    }

    // 获取支付方式
    public function getPayType($payType)
    {
        return $this->payType[$payType];
    }

    // 获取订单状态
    public function getOrderStatus($orderStatus , $paid = 0)
    {
        $status = $this->orderStatus[$orderStatus];
        if(!$paid){
            $status = '未支付';
        }
        /*
         * case 0://未支付
                    $query->where('paid', 0)->where('status', 0)->where('refund_status', 0)->where('is_del', 0);
                    break;
                case 1://已支付 未发货
                    $query->where('paid', 1)->whereIn('status', [0, 4])->whereIn('refund_status', [0, 3])->when(isset($where['shipping_type']), function ($query) {
                        $query->where('shipping_type', 1);
                    })->where('is_del', 0);
                    break;
                case 7://已支付 部分发货
                    $query->where('paid', 1)->where('status', 4)->whereIn('refund_status', [0, 3])->where('is_del', 0);
                    break;
                case 2://已支付  待收货
                    $query->where('paid', 1)->where('status', 1)->whereIn('refund_status', [0, 3])->where('is_del', 0);
                    break;
                case 3:// 已支付  已收货  待评价
                    $query->where('paid', 1)->where('status', 2)->whereIn('refund_status', [0, 3])->where('is_del', 0);
                    break;
                case 4:// 交易完成
                    $query->where('paid', 1)->where('status', 3)->whereIn('refund_status', [0, 3])->where('is_del', 0);
                    break;
                case 5://已支付  待核销
                    $query->where('paid', 1)->where('status', 0)->where('refund_status', 0)->where('shipping_type', 2)->where('is_del', 0);
                    break;
                case 6://已支付 已核销 没有退款
                    $query->where('paid', 1)->where('status', 2)->where('refund_status', 0)->where('shipping_type', 2)->where('is_del', 0);
                    break;
                case -1://退款中
                    $query->where('paid', 1)->whereIn('refund_status', [1, 4])->where('is_del', 0);
                    break;
                case -2://已退款
                    $query->where('paid', 1)->where('refund_status', 2)->where('is_del', 0);
                    break;
                case -3://退款
                    $query->where('paid', 1)->whereIn('refund_status', [1, 2, 4])->where('is_del', 0);
                    break;
                case -4://已删除
                    $query->where('is_del', 1);
                    break;*/
        return $status;
    }
    // 获取支付方式
    public function getOrderTypes($orderTypes)
    {
        return $this->orderTypes[$orderTypes];
    }

    // 获取有效订单数
    public function getOrderNum($storeId = 0)
    {
        $where['is_system_del'] = 0;
        $where['is_del'] = 0;
        if($storeId){
            $where['store_id'] = $storeId;
        }
        $res = self::where($where)->count();
        return $res;
    }

    // 获取待支付订单数
    public function getWaitPayNum()
    {
        $whereArr[] = ['refund_status', '=', 0];
        $whereArr[] = ['is_del', '=', 0];
        $whereArr[] = ['paid', '=', 0];
        $whereArr[] = ['status', '=', 0];
        $num = self::where($whereArr)->count();
        return $num;
    }
    // 获取待发货订单数
    public function getWaitDeliverNum()
    {
        $whereArr[] = ['refund_status', '=', 0];
        $whereArr[] = ['is_del', '=', 0];
        $whereArr[] = ['paid', '=', 1];
        $whereArr[] = ['status','in',[0,1,2,3,4]];
        $whereArr[] = ['delivery_id', '=',''];
        $num = self::where($whereArr)->count();
        return $num;
    }
    // 获取待收货订单数
    public function getWaitReceivedNum()
    {
        $whereArr[] = ['refund_status', '=', 0];
        $whereArr[] = ['is_del', '=', 0];
        $whereArr[] = ['paid', '=', 1];
        $whereArr[] = ['status','=',1];
        $num = self::where($whereArr)->count();
        return $num;
    }
    // 获取已完成订单数
    public function getCompletedNum()
    {
        $whereArr[] = ['refund_status', '=', 0];
        $whereArr[] = ['is_del', '=', 0];
        $whereArr[] = ['paid', '=', 1];
        $whereArr[] = ['status','>=',2];
        $num = self::where($whereArr)->count();
        return $num;
    }
}